package com.example.demo.controller;

import com.example.demo.dao.AccountInterface;
import com.example.demo.dao.UserDaoInterface;
import com.example.demo.entity.Account;
import com.example.demo.entity.User;
import com.example.demo.entity.UserMapper;
import com.example.demo.entity.YearDetail;
import com.example.demo.service.ExportService;
import com.example.demo.util.CollectionResponse;
import io.swagger.annotations.Api;
import net.sf.json.JSONArray;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api("controller相关的添加支出和收入的api")
@RestController
@RequestMapping("/Account")
public class AccountController {

    @Autowired
    AccountInterface accountInterface;

    @Autowired
    private ExportService exportService;
    @Autowired
    private UserDaoInterface userDaoInterface;

    //添加支出数据
    @RequestMapping("/OutAdd")
    public int OutAdd(Account account) {
        int row=0;
        row=accountInterface.OutAdd(account);
        System.out.println("row："+row);
        if(row==1){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return row;
    }

    //添加收入数据
    @RequestMapping("/InAdd")
    public int InAdd(Account account) {
        int row=0;
        row=accountInterface.OutAdd(account);
        System.out.println("row："+row);
        if(row==1){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return row;
    }

    //展示当日记账列表
    @RequestMapping(value ="/DayList", produces = "application/json; charset=utf-8")
    public CollectionResponse DayList(String year, String month, String day, String mainId) {
        int years,months,days,mainIds;
        JSONArray json=null;
        CollectionResponse collectionDay = new CollectionResponse();
        if(year!=null && month!=null && day!=null && mainId!=null){
            years=Integer.parseInt(year);
            months=Integer.parseInt(month);
            days=Integer.parseInt(day);
            mainIds=Integer.parseInt(mainId);
            List<Map<String, Object>> daylist = accountInterface.selectDay(years,months,days,mainIds);
            json= JSONArray.fromObject(daylist);
            collectionDay.setCode("200");
            collectionDay.setData(json);
            collectionDay.setMessage("当日账单明细");
            System.out.println("当日账单明细:"+json);
            System.out.println("当日账单明细："+collectionDay);

            return collectionDay;
        }
        collectionDay.setCode("200");
        collectionDay.setData(json);
        collectionDay.setMessage("当日账单明细");
        return collectionDay;
    }

    //删除记账数据
    @RequestMapping("/Delete")
    public int Delete(String id) {
        int row=0;
        if(!id.equals("") && id!=null){
            int ids=Integer.parseInt(id);
            row=accountInterface.Delete(ids);
        }
        System.out.println("row："+row);
        if(row==1){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
        return row;
    }


    //查询年份中月的收入和支出列表
    @RequestMapping(value = "/SelectYear", produces = "application/json; charset=utf-8")
    public CollectionResponse mPieChart(String string1, String string2) {
        CollectionResponse collection = new CollectionResponse();
        int years,mainIds;
        JSONArray jsonin=null,jsonout = null;
        years = Integer.parseInt(string1);
        mainIds = Integer.parseInt(string2);
        List<Map<String, Object>> inlist = accountInterface.selectYear(years,1,mainIds);
        System.out.println("年收入总结列表："+inlist);
        List<Map<String, Object>> outlist = accountInterface.selectYear(years,0,mainIds);
        System.out.println("年支出总结列表："+outlist);

        for(int i=0;i<outlist.size();i++){
            inlist.add(outlist.get(i));
        }
        List<YearDetail> list = new ArrayList<>();
        for(int x=1;x<13;x++) {
            boolean isOne = false; //是否月份都要支出和收入
            boolean isFind = false; //是否都没有
            YearDetail yearDetail = new YearDetail();
            for (int i = 0; i < inlist.size(); i++) {

                if (x == Integer.parseInt(String.valueOf(inlist.get(i).get("month")) )) {
                    isFind = true;
                    if (Integer.parseInt(String.valueOf(inlist.get(i).get("kind"))) == 1) {
                        yearDetail.setMonth(x);
                        yearDetail.setOutmoney(0);
                       String str =  String.valueOf(inlist.get(i).get("money"));
                        float v = Float.parseFloat(str);
                        yearDetail.setInmoney(v);
                        isOne = true;
                    } else {
                        if (isOne) {
                            yearDetail.setOutmoney(Float.parseFloat(String.valueOf( inlist.get(i).get("money"))));
                            isOne = false;
                        } else {
                            yearDetail.setMonth(x);
                            yearDetail.setInmoney(0);
                            yearDetail.setOutmoney(Float.parseFloat(String.valueOf( inlist.get(i).get("money"))));
                        }
                    }

                }
            }
            if(!isFind){
                yearDetail.setMonth(x);
                yearDetail.setInmoney(0);
                yearDetail.setOutmoney(0);
            }
            yearDetail.setYuer();
            list.add(yearDetail);
        }
//        return list;
        jsonin= JSONArray.fromObject(list);
        System.out.println("年总结列表："+jsonin);
        collection.setCode("200");
        collection.setData(jsonin);
        collection.setMessage("年收入总结列表");
        System.out.println("年收入总结列表："+collection);
        return collection;
    }


    // 导出excel
    @RequestMapping(value ="/Export", produces = "application/json; charset=utf-8")
    public ResponseEntity<byte[]> exportExcel(String year, String month, String kind, String mainId) throws JSONException {
        int years,months,kinds,mainIds;
        years = Integer.parseInt(year);
        mainIds = Integer.parseInt(mainId);
        kinds = Integer.parseInt(kind);
        if(month != null && !month.equals("")){
            months = Integer.parseInt(month);
        }else {
            months=0;
        }
        String email= userDaoInterface.selectEmail(mainIds);
        //listmap 转换为 JSONArray 强转为list对象
        List<Map<String, Object>>  listMap = accountInterface.selectAll(years,months,mainIds);
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(listMap);
        List<Account> list = jsonArray.toList(jsonArray,Account.class);
        return exportService.exportExcel(list,email);
    }


}
